package com.fortune.service.impl;

import com.fortune.dao.DStoreSurplusDao;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusAddDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusEditDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusInfoDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusListDto;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.DStoreSurplusService;
import com.fortune.vo.dStoreSurplus.DStoreSurplusInfo;
import com.fortune.vo.dStoreSurplus.DStoreSurplusInfoDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;


@Service
public class DStoreSurplusServiceImpl implements DStoreSurplusService {
    @Autowired
    private DStoreSurplusDao dStoreSurplusDao;


    @Override
    public int addSurplusBase(String companyId, String dealerId, String uid, DStoreSurplusAddDto addDto) {
        String uuid= UUID.randomUUID().toString().replace("-", "");
        int addSurplusBase=dStoreSurplusDao.addSurplusBase(companyId,dealerId,uuid,uid,addDto);
        if(addSurplusBase>0){
            int addSurplusDetail=0;
            for(DStoreSurplusInfoDto infoDto: addDto.getmStoreSurplusInfoDto()){
                addSurplusDetail=dStoreSurplusDao.addSurplusDetail(companyId,dealerId,uuid,infoDto);
                if(addSurplusDetail<1){
                    throw new InsertInnerErrorException("新增报盈商品失败");
                }
            }
            return addSurplusDetail;
        }
        return addSurplusBase;
    }

    @Override
    public List<DStoreSurplusInfo> findSurplusList(String companyId, String dealerId, DStoreSurplusListDto dStoreSurplusListDto) {
        return dStoreSurplusDao.findDealerSurplusList(companyId,dealerId,dStoreSurplusListDto);
    }

    @Override
    public int findSurplusListCount(String companyId, String dealerId, DStoreSurplusListDto dStoreSurplusListDto) {
        return dStoreSurplusDao.findDealerSurplusListCount(companyId,dealerId,dStoreSurplusListDto);
    }

    @Override
    public DStoreSurplusInfoDetail findDetail(String companyId, String dealerId, String deficitId) {
        return dStoreSurplusDao.findDealerIdSurplusInfo(companyId,dealerId,deficitId);
    }

    @Override
    public int editDStoreSurplusConfirm(String companyId, String dealerId,String operatorId, String uuid) {
        return dStoreSurplusDao.editDStoreSurplusConfirm(companyId, dealerId,operatorId, uuid);
    }

    @Override
    public int deleteDStoreSurplus(String companyId, String dealerId, String uuid) {
        return dStoreSurplusDao.deleteDStoreSurplus(companyId, dealerId, uuid);
    }

    @Override
    public int editSurplusBase(String companyId, String dealerId, DStoreSurplusEditDto editDto) {
        int editSurplusBase=dStoreSurplusDao.editSurplusBase(companyId,dealerId,editDto);
        if(editSurplusBase>0){
            String uuid=editDto.getUuid();
            int addSurplusDetail=0;
            int deleteInfo=dStoreSurplusDao.deleteInfo(companyId,dealerId,uuid);
            if(deleteInfo>0){
                for(DStoreSurplusInfoDto infoDto: editDto.getdStoreDeficitInfoDto()){
                    addSurplusDetail=dStoreSurplusDao.addSurplusDetail(companyId,dealerId,uuid,infoDto);
                    if(addSurplusDetail<1){
                        throw new InsertInnerErrorException("新增报盈商品失败");
                    }
                }
            }

            return addSurplusDetail;

        }
        return editSurplusBase;
    }
}
